package PaperMan// ActionScript file
{
	import MV_BMP.MovieClip_BMP;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;

	/**
	 *位图纸娃娃动画类 
	 * @author upstream
	 * 
	 */	
	public class BMPMC_Cartoon extends MovieClip_BMP
	{
		private var _sourceMC:MovieClip;/*源资源剪辑*/
		private var _drawSign:Boolean;/*绘制标记*/
		private var _partsObj:Object;/*剪辑组成部分*/
		public function BMPMC_Cartoon():void
		{
			
		}
		
		/**
		 *设定资源剪辑 
		 * @param sourceMC
		 * 
		 */		
		public function setSourceMC(sourceMC:MovieClip):void
		{
			this._partsObj=new Object;
			this._sourceMC=sourceMC;
			for(var i:int=0;i<this.sourceMC.numChildren;i++)
			{
				var mcPart:DisplayObject=this.sourceMC.getChildAt(i);
				this.partsObj[mcPart.name]=mcPart;
			}
			this.needDraw();
		}
		
		/**
		 *换肤 
		 * @param partName
		 * @param sourceObj
		 * 
		 */		
		public function changePartSource(partName:String,sourceObj:DisplayObject):void
		{
			var mcPart:DisplayObjectContainer=this.partsObj[partName];
			if(mcPart)
			{
				mcPart.visible=true;
				mcPart.removeChildAt(0);
				mcPart.addChild(sourceObj);
				this.needDraw();
			}
			else
			{
				throw new Error("找不到该部分皮肤");
			}
		}
		
		/**
		 *隐藏皮肤 
		 * @param partName
		 * @param visibled
		 * 
		 */		
		public function changePartInvisibled(partName:String):void
		{
			var mcPart:DisplayObjectContainer=this.partsObj[partName];
			if(mcPart)
			{
				mcPart.visible=false;
				this.needDraw();
			}
			else
			{
				throw new Error("找不到该部分皮肤");
			}
		}
		
		/**
		 *立即绘制动画 
		 * 
		 */		
		public function drawNow():void
		{
			this.draw();
			this.drawSign=false;
		}
		
		/**
		 *需要绘制 
		 * 
		 */		
		public function needDraw():void
		{
			if(this.stage && !this.drawSign)
			{
				this.stage.addEventListener(Event.RENDER,drawHandler);
				this.stage.invalidate();
			}
			this.drawSign=true;
		}
		
		/**
		 *加入舞台事件 
		 * @param arg0
		 * 
		 */		
		override protected function addHandle(arg0:Event):void
		{
			super.addHandle(arg0);
			if(this.drawSign)
			{
			    this.draw();
			    this.drawSign=false;
			}
		}
		
		/**
		 *舞台渲染事件 
		 * @param e
		 * 
		 */		
		private function drawHandler(e:Event):void
		{
			this.draw();
			this.drawSign=false;
			this.stage.removeEventListener(Event.RENDER,drawHandler);
		}
		
		/**
		 *绘制动画 
		 * 
		 */		
		protected function draw():void
		{
			var nowFrame:int=this.currentFrame;
			var stopSign:Boolean=this.isStop;
			this.reset();
			if(this.sourceMC)
			{
				this.turnFrom(this.sourceMC);
				if(stopSign)
				{
					this.gotoAndStop(nowFrame);
				}
				else
				{
					this.gotoAndPlay(nowFrame);
				}
			}
		}

		/*getter & setter*/
		public function get sourceMC():MovieClip
		{
			return _sourceMC;
		}

		public function get drawSign():Boolean
		{
			return _drawSign;
		}

		public function set drawSign(value:Boolean):void
		{
			_drawSign = value;
		}

		public function get partsObj():Object
		{
			return _partsObj;
		}


	}
}